﻿package com.doue.arduino{
	//import mx.events.EventDispatcher;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.net.XMLSocket;
	import flash.events.*;


	//class Arduino extends XMLSocket {
	public class Arduino extends XMLSocket {

		private var _connected:Boolean = false;// 是否已連結
		private var _host:String  = "127.0.0.1";// 主機名稱或是IP位址
		private var _port:int  = 9001;// 設定連結埠號
		//private var socket:XMLSocket;


		//constructor - provide a port number and a host ip adress
		//read the documentation of the SerialProxy to better understandwhat this means
		public function Arduino(port:int = 9001, host:String = "127.0.0.1") {
			//initialize

			//socket = new XMLSocket();
			super();
			//flash.events.EventDispatcher.initialize(this);

			if ((port < 1024) || (port > 65536)) {
				trace("** Arduino ** Port must be from 1024 to 65535 ! read the Flash Documentation and the serProxy config file to better understand");
			} else {
				_port = port;
			}
			//check for host or set default
			_host = host;

			// 重新設定監聽者
			configListener(this);

			//autoconnect
			xconnect();
		}
		// 重新設定監聽者
		private function configListener(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.CLOSE, closeHandler);
			dispatcher.addEventListener(Event.CONNECT, connectHandler);
			dispatcher.addEventListener(DataEvent.DATA, dataHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			//dispatcher.addEventListener('onData', onDataReceived);
			//dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			// dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		}
		//---------------------------------------
		//CONNECT and DISCONNECT + responders
		//---------------------------------------

		//connect to the XMLsocket
		public function xconnect():void {
			super.connect(_host, _port);
		}
		//disconnects from the xmlsocket (not Arduino itself)
		public function disconnect() {
			if (_connected) {
				super.close();
				_connected = false;
			}
		}
		// connect handler
		private function connectHandler(event:Event):void {
			trace("** Arduino ** Connecting to "+_host+":"+_port+" . . .");
		}
		// close handler
		private function closeHandler(event:Event):void {
			trace("** Arduino ** disconnected");
		}
		// data handler
		private function dataHandler(event:DataEvent):void {
			trace("dataHandler:" + event.data);
			//dispatchEvent('onData');
			//onDataReceived(event.data);
			//e_onReceiveData(str);
		}
		// io error handler
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("** Arduino ** Connection failed! you must launch the serialProxy first");
		}
		//---------------------------------------
		//SEND and receive data from server
		//---------------------------------------

		//overrides XMLSocket.onData in order to have pure string and not the full XML object
		private function onDataReceived(str:String) {
			//trace("** Arduino ** onDataReceived str:"+str);
			//launch event
			e_onReceiveData(str);
		}
		/*
		//---------------------------------------
		//EVENTS
		//---------------------------------------
		}
		*/
		
		private function e_onReceiveData(str:String) {
			trace("** Arduino ** onDataReceived str:"+str);
			var evt = {target:this, type:"onReceiveData"};
			evt.data = str;
			dispatchEvent(new Event(evt));
		}
	}
}